package org.nnxy.foodboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.nnxy.foodboot.common.Result;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.entity.UserViewHistory;
import org.nnxy.foodboot.service.UserViewHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户浏览历史控制器
 */
@RestController
@RequestMapping("/view-history")
public class UserViewHistoryController {
    
    @Autowired
    private UserViewHistoryService userViewHistoryService;
    
    /**
     * 记录用户浏览历史
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param viewDuration 浏览时长(秒)
     * @return 操作结果
     */
    @PostMapping("/record")
    public Result<Boolean> recordViewHistory(
            @RequestParam Long userId,
            @RequestParam Long recipeId,
            @RequestParam Integer viewDuration) {
        
        boolean result = userViewHistoryService.recordViewHistory(userId, recipeId, viewDuration);
        return Result.success(result);
    }
    
    /**
     * 查询用户浏览历史
     * @param page 页码
     * @param size 每页数量
     * @param userId 用户ID
     * @return 浏览历史列表
     */
    @GetMapping("/list")
    public Result<IPage<Recipe>> getUserViewHistory(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Long userId) {
        
        Page<Recipe> pageParams = new Page<>(page, size);
        IPage<Recipe> result = userViewHistoryService.pageUserViewHistory(pageParams, userId);
        return Result.success(result);
    }
    
    /**
     * 获取用户对特定食谱的最近浏览历史
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 浏览历史
     */
    @GetMapping("/latest")
    public Result<UserViewHistory> getLatestViewHistory(
            @RequestParam Long userId,
            @RequestParam Long recipeId) {
        
        UserViewHistory viewHistory = userViewHistoryService.getLatestViewHistory(userId, recipeId);
        return Result.success(viewHistory);
    }
    
    /**
     * 清空用户浏览历史
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/clear")
    public Result<Boolean> clearUserViewHistory(@RequestParam Long userId) {
        boolean result = userViewHistoryService.clearUserViewHistory(userId);
        return Result.success(result);
    }
} 